<?php
class ModelCommunityViewforum extends Model{
    public function getForumInfo($getData){
        $encrypt_data=md5($getData['fid']);

        $query=$this->sql_cache->get('viewforum.foruminfo.' . $encrypt_data . '.' . (int)$this->config->get('config_language_id'));

        if(!$query){
            $query=$this->db->query("SELECT f.parent_id, f.forum_enabled, f.forum_type, f.topic_category_enabled, f.topic_category_displayed, fn.forum_name, fpn.forum_name AS parent_forum_name FROM " . DB_PREFIX . "community_forum AS f LEFT JOIN " . DB_PREFIX . "community_forum_name_des_rule AS fn ON (fn.forum_id=f.forum_id AND fn.language_id='" . (int)$this->config->get('config_language_id') . "') LEFT JOIN " . DB_PREFIX . "community_forum_name_des_rule AS fpn ON (fpn.forum_id=f.parent_id AND fpn.language_id='" . (int)$this->config->get('config_language_id') . "') WHERE f.forum_id='" . (int)$getData['fid'] . "' LIMIT 0,1");

            $this->sql_cache->set('viewforum.foruminfo.' . $encrypt_data . '.' . (int)$this->config->get('config_language_id'),$query);
        }

        return $query->row;
    }

    public function getSubForums($getData){
        $encrypt_data=md5($getData['fid']);

        $results=$this->sql_cache->get('viewforum.subforums.' . $encrypt_data . '.' . (int)$this->config->get('config_language_id'));

        if(!$results){
            $results=$this->db->query("SELECT f.forum_id, fn.forum_name, fn.forum_desc, f.forum_topics, f.forum_posts, f.forum_last_poster_id, f.forum_last_poster_name, f.forum_last_post_time FROM " . DB_PREFIX . "community_forum AS f LEFT JOIN " . DB_PREFIX . "community_forum_name_des_rule AS fn ON (fn.forum_id=f.forum_id AND fn.language_id='" . (int)$this->config->get('config_language_id') . "') WHERE f.parent_id='" . (int)$getData['fid'] . "' ORDER BY f.sort_order_id ASC, fn.forum_name ASC");

            if($results->rows){
                $this->sql_cache->set('viewforum.subforums.' . $encrypt_data  . '.' . (int)$this->config->get('config_language_id'),$results);
            }
        }

        if($results->rows){
            $forum_id_str='';

            foreach($results->rows as $result){
                $forum_id_str.=$result['forum_id'] . ',';
            }

            $forum_id_str=rtrim($forum_id_str,',');

            $mark_time_results=$this->db->query("SELECT forum_id, mark_time FROM " . DB_PREFIX . "community_forum_track WHERE forum_id IN (" . $forum_id_str . ") AND user_id='" . (int)$this->customer->getForumUserId() . "'");

            $query=array();

            $i=0;

            foreach($results->rows as $result){
                $query[]=$result;

                foreach($mark_time_results->rows as $mark_time_result){
                    if($mark_time_result['forum_id']==$result['forum_id']){
                        $query[$i]['mark_time']=$mark_time_result['mark_time'];
                    }
                }
                $i++;
            }
        }else{
            $query=$results->rows;
        }

        return $query;
    }

    public function getForumModerators($getData){
        $encrypt_data=md5($getData['fid']);

        $results=$this->sql_cache->get('viewforum.forummod.' . $encrypt_data);

        if(!$results){
            $query=$this->db->query("SELECT forum_mod_ids FROM " . DB_PREFIX . "community_forum WHERE forum_id='" . $getData['fid'] . "' LIMIT 0,1");

            $forum_mod_ids=array();

            if(!empty($query->row['forum_mod_ids'])){
                $forum_mod_ids=unserialize($query->row['forum_mod_ids']);
            }

            $forum_mod_id_str='';

            foreach($forum_mod_ids as $forum_mod_id){
                $forum_mod_id_str.=$forum_mod_id . ',';
            }

            $forum_mod_id_str=rtrim($forum_mod_id_str,',');

            if($forum_mod_id_str){
                $results=$this->db->query("SELECT user_id, username FROM " . DB_PREFIX . "community_users WHERE user_id IN (" . $forum_mod_id_str . ")");
            }else{
                $results=array();
            }

            $this->sql_cache->set('viewforum.forummod.' . $encrypt_data,$results);
        }

        if(is_array($results)){
            return $results;
        }else{
            return $results->rows;
        }
    }

    public function getTopicCats($getData){
        $encrypt_data=md5($getData['fid']);

        $query=$this->sql_cache->get('viewforum.topiccats.' . $encrypt_data . '.' . (int)$this->config->get('config_language_id'));

        if(!$query){
            $query=$this->db->query("SELECT c.topic_category_id, ct.topic_category_title FROM " . DB_PREFIX . "community_topic_category AS c LEFT JOIN " . DB_PREFIX . "community_topic_category_title AS ct ON (ct.topic_category_id=c.topic_category_id AND ct.language_id='" . (int)$this->config->get('config_language_id') . "') WHERE c.forum_id=" . (int)$getData['fid'] . " ORDER BY c.sort_order_id ASC");

            $this->sql_cache->set('viewforum.topiccats.' . $encrypt_data . '.' . (int)$this->config->get('config_language_id'),$query);
        }

        return $query->rows;
    }
    
    public function getTopicTotal($getData){
        $encrypt_data=md5($getData['fid']);

        if(isset($getData['filter'])){
            if(isset($getData['tcid'])){
                $query=$this->sql_cache->get('viewforum.topictotal.' . $encrypt_data . '.' . $getData['filter'] . '.' . $getData['tcid']);
            }else{
                $query=$this->sql_cache->get('viewforum.topictotal.' . $encrypt_data . '.' . $getData['filter']);
            }
        }else{
            $query=$this->sql_cache->get('viewforum.topictotal.' . $encrypt_data);
        }

        if(!$query){
            $sql="SELECT COUNT(*) AS total FROM " . DB_PREFIX . "community_topics WHERE forum_id=" . (int)$getData['fid'] . " AND topic_approved='1' AND topic_type='0' AND topic_deleted='0'";

            if(isset($getData['filter'])){
                switch($getData['filter']){
                    case 'feature':
                        $sql.=" AND topic_featured='1'";
                        break;
                    case 'poll':
                        $sql.=" AND topic_polled='1'";
                        break;
                    case 'tcid':
                        $sql.=" AND topic_category_id='" . $getData['tcid'] . "'";
                        break;
                }
            }

            $query=$this->db->query($sql);

            if(isset($getData['filter'])){
                if(isset($getData['tcid'])){
                    $this->sql_cache->set('viewforum.topictotal.' . $encrypt_data . '.' . $getData['filter'] . '.' . $getData['tcid'],$query,TRUE);
                }else{
                    $this->sql_cache->set('viewforum.topictotal.' . $encrypt_data . '.' . $getData['filter'],$query,TRUE);
                }
            }else{
                $this->sql_cache->set('viewforum.topictotal.' . $encrypt_data,$query,TRUE);
            }
        }

        return $query->row['total'];
    }
 
     public function getTopics($getData,$start,$limit){
         $encrypt_data=md5($getData['fid']);

         if(isset($getData['filter'])){
             if(isset($getData['tcid'])){
                 $results=$this->sql_cache->get('viewforum.topics.' . $encrypt_data . '.' . $getData['filter'] . '.' . $getData['tcid'] . '.' . $start .'-' .$limit . '.' . (int)$this->config->get('config_language_id'));
             }else{
                 $results=$this->sql_cache->get('viewforum.topics.' . $encrypt_data . '.' . $getData['filter'] . '.' . $start .'-' .$limit . '.' . (int)$this->config->get('config_language_id'));
             }
         }else{
             $results=$this->sql_cache->get('viewforum.topics.' . $encrypt_data . '.' . $start .'-' .$limit . '.' . (int)$this->config->get('config_language_id'));
         }

         if(!$results){
             if(isset($getData['filter'])){
                 switch($getData['filter']){
                     case 'feature':
                         $filterOption=" AND t.topic_featured='1'";
                         break;
                     case 'poll':
                         $filterOption=" AND t.topic_polled='1'";
                         break;
                     case 'tcid':
                         $filterOption=" AND t.topic_category_id='" . (int)$getData['tcid'] . "'";
                         break;
                     default:
                         $filterOption='';
                 }
             }else{
                 $filterOption='';
             }

             $sql="SELECT t.topic_id, t.topic_title, t.topic_time, t.topic_replies, t.topic_status, t.topic_type, t.topic_time_limit, t.topic_views, t.topic_last_poster_name, t.topic_last_poster_id, t.topic_last_post_time, t.forum_id, t.topic_category_id, t.topic_featured, t.topic_reported, t.topic_solved, tr.rating, u.user_id, u.username, tc.topic_category_title, pr.image FROM " . DB_PREFIX . "community_topics AS t LEFT JOIN " . DB_PREFIX . "community_topic_review AS tr ON (tr.topic_id=t.topic_id AND tr.post_id=t.topic_first_post_id) LEFT JOIN " . DB_PREFIX . "community_users AS u ON (t.topic_poster_id=u.user_id) LEFT JOIN " . DB_PREFIX . "community_topic_category_title AS tc ON (tc.topic_category_id=t.topic_category_id AND tc.language_id='" . (int)$this->config->get('config_language_id') . "') LEFT JOIN " . DB_PREFIX . "product AS pr ON (t.cart_product_id=pr.product_id) WHERE (t.forum_id=" . (int)$getData['fid'] . " AND t.topic_approved='1' AND (t.topic_type='0' OR t.topic_type='1') AND t.topic_deleted='0'" . $filterOption . ") ORDER BY t.topic_type DESC, t.topic_last_post_time DESC LIMIT " . $start . "," . $limit;

             $results=$this->db->query($sql);

             if(isset($getData['filter'])){
                 if(isset($getData['tcid'])){
                     $this->sql_cache->set('viewforum.topics.' . $encrypt_data . '.' . $getData['filter'] . '.' . $getData['tcid'] . '.' . $start .'-' .$limit . '.' . (int)$this->config->get('config_language_id'),$results,TRUE);
                 }else{
                     $this->sql_cache->set('viewforum.topics.' . $encrypt_data . '.' . $getData['filter'] . '.' . $start .'-' .$limit . '.' . (int)$this->config->get('config_language_id'),$results,TRUE);
                 }
             }else{
                 $this->sql_cache->set('viewforum.topics.' . $encrypt_data . '.' . $start .'-' .$limit . '.' . (int)$this->config->get('config_language_id'),$results,TRUE);
             }
         }

         $topic_id_str='';

         foreach($results->rows as $result){
             $topic_id_str.=$result['topic_id'] . ',';
         }

         $topic_id_str=rtrim($topic_id_str,',');

         if($topic_id_str!=''){
             $mark_time_results=$this->db->query("SELECT topic_id, mark_time FROM " . DB_PREFIX . "community_topics_track WHERE topic_id IN (" . $topic_id_str . ") AND user_id='" . (int)$this->customer->getForumUserId() . "'");

             $query=array();

             $i=0;

             foreach($results->rows as $result){
                 $query[]=$result;

                 foreach($mark_time_results->rows as $mark_time_result){
                     if($mark_time_result['topic_id']==$result['topic_id']){
                         $query[$i]['mark_time']=$mark_time_result['mark_time'];
                     }
                 }
                 $i++;
             }

             return $query;
         }
    }
        
    public function getAnnouncementTopics($getData){
        $encrypt_data=md5($getData['fid']);

        $results=$this->sql_cache->get('viewforum.announcementtopics.' . $encrypt_data);

        if(!$results){
            $results=$this->db->query("SELECT t.topic_id, t.topic_title, t.topic_time, t.topic_replies, t.topic_views, t.topic_status, t.topic_type, t.topic_time_limit, t.topic_last_poster_name, t.topic_last_poster_id, t.topic_last_post_time, t.forum_id, t.topic_featured, t.topic_reported, u.user_id, u.username, pr.image FROM " . DB_PREFIX . "community_topics AS t LEFT JOIN " . DB_PREFIX . "community_users AS u ON (t.topic_poster_id=u.user_id ) LEFT JOIN " . DB_PREFIX . "product AS pr ON (t.cart_product_id=pr.product_id) WHERE (t.forum_id=" . (int)$getData['fid'] . " AND t.topic_approved='1' AND t.topic_type='2' AND t.topic_deleted='0') OR (t.topic_approved='1' AND t.topic_type='3' AND t.topic_deleted='0') ORDER BY t.topic_type DESC, t.topic_last_post_time DESC");

            $this->sql_cache->set('viewforum.announcementtopics.' . $encrypt_data,$results);
        }

        $topic_id_str='';

        foreach($results->rows as $result){
            $topic_id_str.=$result['topic_id'] . ',';
        }

        $topic_id_str=rtrim($topic_id_str,',');

        if($topic_id_str!=''){
            $mark_time_results=$this->db->query("SELECT topic_id, mark_time FROM " . DB_PREFIX . "community_topics_track WHERE topic_id IN (" . $topic_id_str . ") AND user_id='" . (int)$this->customer->getForumUserId() . "'");

            $query=array();

            $i=0;

            foreach($results->rows as $result){
                $query[]=$result;

                foreach($mark_time_results->rows as $mark_time_result){
                    if($mark_time_result['topic_id']==$result['topic_id']){
                        $query[$i]['mark_time']=$mark_time_result['mark_time'];
                    }
                }
                $i++;
            }

            return $query;
        }
    }

    //update forum track
    public function updateForumTrack($getData){
        if($this->customer->isLogged()){
            $this->db->query("SELECT 1 FROM " . DB_PREFIX . "community_forum_track WHERE forum_id='" . (int)$getData['fid'] . "' AND user_id='" . (int)$this->customer->getForumUserId() . "' LIMIT 0,1");

            if($this->db->countAffected()){
                $query=$this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "community_topics WHERE forum_id='" . (int)$getData['fid'] . "' AND topic_last_post_time>='" . (int)(time()-$this->config->get('topics_be_read_time')) . "' AND topic_deleted='0' AND topic_approved='1'");
                $topicNum=$query->row['total'];

                $query=$this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "community_topics_track AS tt LEFT JOIN " . DB_PREFIX . "community_topics AS t ON (t.topic_id=tt.topic_id) WHERE tt.forum_id='" . (int)$getData['fid'] . "' AND tt.user_id='" . (int)$this->customer->getForumUserId() . "' AND tt.mark_time>t.topic_last_post_time");
                $topicTrackNum=$query->row['total'];

                if($topicTrackNum>=$topicNum){
                    $this->db->query("UPDATE " . DB_PREFIX . "community_forum_track SET mark_time='" . (int)time() . "' WHERE forum_id='" . (int)$getData['fid'] . "' AND user_id='" . (int)$this->customer->getForumUserId() . "'");
                }
            }else{
                $this->db->query("INSERT INTO " . DB_PREFIX . "community_forum_track SET user_id='" . (int)$this->customer->getForumUserId() . "', forum_id='" . (int)$getData['fid'] . "', mark_time='" . (int)time() . "'");
            }
        }
    }

    public  function deleteTopicsTrack($topicIds){
        $this->db->query("DELETE FROM " . DB_PREFIX . "community_topics_track WHERE topic_id IN (" . $topicIds . ") AND user_id='" . (int)$this->customer->getForumUserId() . "'");
    }


    //subscribe forum
    public function subsribeForum($getData){
        $this->db->query("INSERT INTO " . DB_PREFIX . "community_forum_notify SET user_id='" . (int)$this->customer->getForumUserId() . "', forum_id='" . (int)$getData['fid'] . "', notify_status='0'");
    }

    public function DetectSubsribeForum($getData){
        $query=$this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "community_forum_notify WHERE forum_id='" . (int)$getData['fid'] . "' AND user_id='" . (int)$this->customer->getForumUserId() . "' LIMIT 0,1");

        return $query->row['total'];
    }

    public function unsubscribeForum($getData){
        $this->db->query("DELETE FROM " . DB_PREFIX . "community_forum_notify WHERE forum_id='" . (int)$getData['fid'] . "' AND user_id='" . (int)$this->customer->getForumUserId() . "'");
    }

    public function updateForumNotification($getData){
        $query=$this->db->query("SELECT notify_status FROM " . DB_PREFIX . "community_forum_notify WHERE forum_id='" . (int)$getData['fid'] . "' AND user_id='" . (int)$this->customer->getForumUserId() . "' LIMIT 0,1");

        if(isset($query->row['notify_status']) && $query->row['notify_status']==1){
            $this->db->query("UPDATE " . DB_PREFIX . "community_forum_notify SET notify_status='0' WHERE forum_id='" . (int)$getData['fid'] . "' AND user_id='" . (int)$this->customer->getForumUserId() . "'");
        }
    }

    public function UpdateTopicNormalType($topic_id){
        $this->db->query("UPDATE " . DB_PREFIX . "community_topics SET topic_type='0', topic_time_limit='0' WHERE topic_id='" . (int)$topic_id . "'");
    }
}
?>